<?php

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Validation\ValidationException;
class LoginRequest extends FormRequest
{
    protected function failedValidation(Validator $validator)
    {
        // 此处自定义表单验证错误信息
        $data = [
            'code' => 201,
            'msg' => $validator->errors()->first(),
        ];
        $respone = new Response(json_encode($data));
        throw (new ValidationException($validator, $respone))
            ->errorBag($this->errorBag)
            ->redirectTo($this->getRedirectUrl());
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        $rule = [
            'captcha' => 'required|captcha_api:'. request('key') . ',math',
            'username' => 'required|string|min:1|alpha_dash',
            'password' => 'required|string|min:6|',
        ];
        return $rule;
    }
    public function messages()
    {
        return [
            'unique'=>':attribute 手机号码已经存在',
            'required'=>':attribute 为必填项',
            'min'=>':attribute 长度最少 :min ,不符合要求',
            'captcha.captcha_api'=>'验证码填写错误',
        ];

    }
    public function authorize()
    {
        return true;
    }
}
